package com.hmz.backend.service.Impl.notice;

import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hmz.backend.entity.Notice;
import com.hmz.backend.mapper.NoticeMapper;
import com.hmz.backend.service.notice.NoticeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Timestamp;
import java.util.List;


@Service
public class NoticeServiceImpl implements NoticeService {
    @Autowired
    private NoticeMapper noticeMapper;
    @Override
    public JSONObject getNotice(int pageNow, int pageSize) {
        IPage<Notice> page = new Page<>(pageNow,pageSize);
        QueryWrapper<Notice> queryWrapper = new QueryWrapper<>();
        queryWrapper.orderByDesc("datetime");
        IPage<Notice> res = noticeMapper.selectPage(page,queryWrapper);
        List<Notice> list = res.getRecords();
        long total = res.getTotal();
        return new JSONObject().set("total",total).set("list",list);
    }
    @Override
    public JSONObject sendNotice(JSONObject data) {
        Notice notice = new Notice();
        notice.setPersonName(data.get("personName").toString());
        notice.setTitle(data.get("title").toString());
        notice.setContent(data.get("content").toString());
        notice.setDateTime(new Timestamp(System.currentTimeMillis()));
        try {
            int insert = noticeMapper.insert(notice);
        }catch (Exception e) {
            return new JSONObject().set("message","发布公告失败");
        }
        return new JSONObject().set("message","发布公告成功");
    }

}
